import { getShopDetailInfo, getCounsellorInfo } from '../../apis/index'
const appData = getApp()

Page({
  data: {
    autoplay: true,
    yyfw: [{
      label: '无痕衣裤',
      type: '果冻条'
    }, {
      label: '无痕衣裤',
      type: '果冻条'
    }, {
      label: '无痕衣裤',
      type: '果冻条'
    }],
    yyfsMsgPic: [
      {
        label: '2排扣'
      },
      {
        label: '3排扣'
      },
      {
        label: '4排扣'
      },
    ],
    num: 0,
    detailData: {
      name: '',
      modelCode: '',
      brandName: '',
      typeName: '',
      price: '',
      produceTime: '',
      counsellorName: '',
      counsellorPhone: '',
      imageItemVOList:[],
      remark: {
        introduceItemVOList: [],
        paramItemVOList: [],
        scopeItemVOList: {
          text: "",
          picText: [],
          textText: []
        }
      }
    }
  },
  onLoad(options) {
    this.getDetail(options.id)
   
  },
  async getCurrentCounsellorInfo() {
    console.log(appData.globalData.currentAddress);
      const par = {
        addressName: appData.globalData.currentAddress
      }
      const res = await getCounsellorInfo(par)
      const { counsellorItemVOList } = res;
      let counsellorName = '';
      let counsellorPhone = '';
      if (Array.isArray(counsellorItemVOList) && counsellorItemVOList.length) {
        counsellorName = counsellorItemVOList[0].counsellorName
        counsellorPhone = counsellorItemVOList[0].counsellorPhone
      }
      console.log('gggg', counsellorName,
      counsellorPhone,);
      if (counsellorName && counsellorPhone) {
        console.log(789);
        const data1 = {
          num: 0,
          detailData: {
            ...this.data.detailData,
            counsellorName,
            counsellorPhone,
          }
        }
        console.log(data1);
        this.setData(data1)
      }

    
    // else {
    //   if (this.data.num > 3) {
    //     return
    //   }
    //   setTimeout(() => {
    //     this.setData({
    //       num: this.data.num + 1
    //     })
    //     this.getCurrentCounsellorInfo()
    //   }, 400);
    // }

  },
  preview(e) {
    const { detail, target, currentTarget } = e
    const { index } = currentTarget.dataset
    wx.previewImage({
      current: this.data.imageItemVOList[index].url, // 当前显示图片的http链接
      urls: this.data.imageItemVOList.map(item => item.url) // 需要预览的图片http链接列表
    })
  },
  onPreview(e) {
    const { detail, target, currentTarget } = e
    const { index } = currentTarget.dataset
    const url = this.data.detailData.remark.scopeItemVOList.picText[index].picUrl
    wx.previewImage({
      current: url, // 当前显示图片的http链接
      urls: this.data.detailData.remark.scopeItemVOList.picText.map(item => item.picUrl) // 需要预览的图片http链接列表
    })
  },
  async getDetail(id) {
    const res = await getShopDetailInfo({
      id
    })
    const {
      remark = '',
      name,
      modelCode,
      brandName,
      typeName,
      price,
      produceTime,
      counsellorName,
      counsellorPhone,
      imageItemVOList
    } = res;
    const parseRemark = remark ? JSON.parse(remark) : {}
    this.setData({
      detailData: {
        name,
        modelCode,
        brandName,
        typeName,
        price,
        produceTime,
        counsellorName,
        counsellorPhone,
        remark: parseRemark,
      },
      imageItemVOList
    })
    // parseIntroductionInfoList
    this.getCurrentCounsellorInfo()
  }
})